package cn.gyouzhe.source;

import cn.gyouzhe.component.disruptor.DisruptorComponent;
import cn.gyouzhe.source.vo.GoodVo;

import static cn.gyouzhe.source.handlers.HandlerFactory.*;

/**
 * 生产者
 *
 * @author wangchuan
 * @datetime 2018/2/13 0:07
 */
class Productor {
    private DisruptorComponent disruptor;

    private Productor() {

    }

    static class InstanceHolder {
        static Productor instance = new Productor();
    }

    public static Productor getInstance() {
        return InstanceHolder.instance;
    }

    private DisruptorComponent getDisruptor() {
        if(null == disruptor) {
            synchronized(this) {
                if(null == disruptor) {
                    disruptor = DisruptorComponent.create()
                            .ringBufferSize(2048)
                            .handlers(sellerStoreHandler())
                            .then(couponStoreHandler())
                            .then(goodStoreHandler())
                            .build();
                }
            }
        }
        return disruptor;
    }

    public void publish(GoodVo goodVo) {
        getDisruptor().publish(goodVo);
    }
}
